#!/usr/bin/env python
#
# Copyright 2007, Doug Hellmann
#
"""Views for browsing.
"""

__version__ = "$Id$"

# Import system modules
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response

import datetime
import logging

# Import local modules
from models import *
from docket import utils

# Module

logger = logging.getLogger('docket.browse')

def decade_list(request):
    logger.debug('decade_list')
    results = utils.query_to_dicts("""
    SELECT
        (year/10)*10 as decade,
        count(*) as case_count
    FROM
        docket_case
    GROUP BY
        decade
    ORDER BY
        decade
    """)
    return render_to_response('arrest_decade_list.html',
                              {'decades':results,
                               },
                              )

def decade(request, decade):
    logger.debug('decade')
    decade = int(decade)
    results = utils.query_to_dicts("""
    SELECT
        year,
        count(*) as case_count
    FROM
        docket_case
    WHERE
        year/10 = %s
    GROUP BY
        year
    ORDER BY
        year
    """, decade/10)
    
    template_args = {'years':results,
                     'decade_start':decade,
                     'decade_end':decade + 9,
                     }

    # Only include next/previous dates if there is data
    # in that direction.
    decade_start = datetime.datetime(year=decade, month=1, day=1)
    next_decade_start = decade_start.replace(year=decade + 10)
    any_previous = Case.objects.filter(year__lt=decade_start.year).count()
    if any_previous:
        template_args['prev_decade'] = decade - 10
        template_args['prev_decade_end'] = decade - 1
    any_next = Case.objects.filter(year__gte=next_decade_start.year).count()
    if any_next:
        template_args['next_decade'] = decade + 10
        template_args['next_decade_end'] = decade + 19

    return render_to_response('arrest_decade.html', template_args)

def compute_next_and_prev_dates(this_month):
    """Return a dictionary with next_year, next_month, this_year,
    this_month, prev_year, prev_month.
    """

    if this_month.month == 1:
        previous_month = this_month.replace(year=this_month.year-1, month=12)
    else:
        previous_month = this_month.replace(month=this_month.month-1)

    if this_month.month == 12:
        next_month = this_month.replace(year=this_month.year+1, month=1)
    else:
        next_month = this_month.replace(month=this_month.month+1)

    month = this_month
    year = this_month.replace(month=1, day=1)
    decade = year.replace(year=(year.year/10)*10)
    next_year = this_month.replace(year=this_month.year+1)
    previous_year = this_month.replace(year=this_month.year-1)

    month_list = utils.months_in_year(year.year)
    
    return locals()

def month(request, year, month):
    """Show the cases in a full month.
    """
    logger.debug('month(%s, %s)', year, month)
    year = int(year)
    month = int(month)

    this_month = datetime.date(year, month, 1)
    dates = compute_next_and_prev_dates(this_month)

    # Only include next/previous dates if there is data
    # in that direction.
    any_previous = Case.objects.filter(arrest_date__lte=this_month).count()
    if not any_previous:
        del dates['previous_year']
        del dates['previous_month']
    any_next = Case.objects.filter(arrest_date__gte=dates['next_month']).count()
    if not any_next:
        del dates['next_year']
        del dates['next_month']

    # Find the cases for the first month of the year
    cases = Case.objects.filter(
        arrest_date__year=this_month.year,
        arrest_date__month=this_month.month,
        )
    num_cases = cases.count()
    logger.debug('%d cases', num_cases)

    render_args = { 'case_list':cases,
                    'num_cases':num_cases,
                    }

    render_args.update(dates)
    
    return render_to_response('arrest_month.html', render_args)
